#作业一、利用Python实现一个多线程程序

import requests
from threading import Thread
from multiprocessing import Process

#模拟请求知乎网：https://www.zhihu.com/

result = []
def request_baidu(index):
    url = "https://www.baidu.com/"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"
    }
    response = requests.get(url=url, headers=headers)
    print(f"当前请求序列号：{index}, 返回状态码：{response.status_code}")
    result.append(response)

if __name__ == "__main__":
    thread_array = []
    for i in range(20):
        t = Thread(target = request_baidu, args=(i,))   #生成线程对象
        thread_array.append(t)
        t.start()  #启动线程
    for t in thread_array:
        t.join()
    print("任务结束")
    print(result)


#作业二、将多线程程序改为多进程程序

# if __name__ == "__main__":
#     process_array = []
#     for i in range(20):
#         p = Process(target=request_baidu, args=(i,))   #生成进程对象
#         process_array.append(p)
#         p.start()  #启动线程
#     for p in process_array:
#         p.join()
#     print("任务结束")